import Cookies from 'js-cookie'
import Vue from 'vue'
import { APP_THEME, APP_BACKGOURND, APP_OPACITY, APP_BACKGOURND_HTTP, SET_DRAGGABLE_OBJECT } from '../mutation-types'
import { getReceptionSkin } from '@/api/system/config'
const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  size: Cookies.get('size') || 'medium',
  color: '#1890FF',
  bgPath: '',
  httpBgPath: '',
  opacity: 0,
  draggableObj: null
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_SIZE: (state, size) => {
    state.size = size
    Cookies.set('size', size)
  },
  [APP_THEME]: (state, color) => {
    state.color = color
  },
  [APP_BACKGOURND]: (state, path) => {
    state.bgPath = path
  },
  [APP_OPACITY]: (state, opacity) => {
    state.opacity = opacity
  },
  [APP_BACKGOURND_HTTP]: (state, path) => {
    state.httpBgPath = path
  },
  [SET_DRAGGABLE_OBJECT]: (state, obj) => {
    state.draggableObj = obj
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  },
  setColor({ commit }, color) {
    Vue.ls.set(APP_THEME, color)
    commit(APP_THEME, color)
  },
  setBg({ commit }, path) {
    Vue.ls.set(APP_BACKGOURND, path)
    commit(APP_BACKGOURND, path)
  },
  setHttpBg({ commit }, path) {
    Vue.ls.set(APP_BACKGOURND_HTTP, path)
    commit(APP_BACKGOURND_HTTP, path)
  },
  setOpacity({ commit }, opacity) {
    Vue.ls.set(APP_OPACITY, opacity)
    commit(APP_OPACITY, opacity)
  },
  setDraggableObj({ commit }, obj) {
    commit(SET_DRAGGABLE_OBJECT, obj)
  },
  InitSkin({ commit }, info) {
    return new Promise((resolve, reject) => {
      getReceptionSkin()
        .then(res => {
          // 主题色
          const color = res.data.skinSubjectColor || '#1890FF'
          const bgUrl = res.data.skinUrl || ''
          const skinId = res.data.defaultStting || -1
          const opacity = res.data.skinTransparency || 0
          const skinType = res.data.skinType
          commit(APP_THEME, color)
          commit(APP_OPACITY, Number(opacity))
          commit(APP_BACKGOURND_HTTP, bgUrl || '')
          // 网络图片
          if (skinType && Number(skinType) === 2) {
            commit(APP_BACKGOURND, bgUrl)
          }
          // 本地图片
          if (skinType && Number(skinType) === 1) {
            commit(APP_BACKGOURND, skinId !== -1 ? Number(skinId) : '')
          }
          resolve(true)
        })
        .catch(() => resolve(false))
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
